﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;

namespace BasicDataManagement
{
    /// <summary>
    /// AddMaterialItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddMaterialItemWindow : Window
    {
        public ObservableCollection<string> Cats { get; set; }
        public string DefaultCat { get; set; }

        public EditState state;

        public MaterialObject materialObject { get; set; }

        public AddMaterialItemWindow()
        {
            InitializeComponent();
            state = EditState.Uknown;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            if (materialObject == null)
            {
                materialObject = new MaterialObject();
                catListCheck.Text = "日盘点";

            }
            catListCombo.ItemsSource = Cats;
            catListCombo.SelectedIndex = Cats.IndexOf(materialObject.BelongedCategoryName);
            if (materialObject.CheckProperty == 1) {
                catListCheck.Text = "日盘点";
            }
            else if (materialObject.CheckProperty == 2)
            {
                catListCheck.Text = "周盘点";
            }
            else if (materialObject.CheckProperty == 3)
            {
                catListCheck.Text = "月盘点";
            }
            nameTB.Text = materialObject.ObjectName;
            unitTB.Text = materialObject.Unit;
            //catListCombo.Text = materialObject.BelongedCategoryName;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (catListCombo.Text == "" || nameTB.Text == "")
            {
                MessageBox.Show("请填写完整信息");
                return;
            }
            if (catListCheck.Text == "日盘点")
            {
                materialObject.CheckProperty = 1;
            }
            else if (catListCheck.Text == "周盘点")
            {
                materialObject.CheckProperty = 2;
            }
            else if (catListCheck.Text == "月盘点")
            {
                materialObject.CheckProperty = 3;
            }
            materialObject.BelongedCategoryName = catListCombo.Text;
            materialObject.ObjectName = nameTB.Text;
            materialObject.Unit = unitTB.Text;
            this.DialogResult = true;

            if (state == EditState.Create)
            {
                //AddObject( "Material", materialObject );
                MessageBox.Show("添加完成");
            }
            else if (state == EditState.Modify)
            {
                //UpdateObject( "Material", materialObject );
                MessageBox.Show("编辑完成");
            }
            else
            {
                MessageBox.Show("未知状态");
            }

            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

    }
}
